import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../views/Login.vue'
// const Login = ()=> import(/* webpackChunkName:" 打包后生成的js文件名 " */ '../views/Login.vue');
// import Home from '../views/Home.vue'
// import Welcome from '../components/Welcome.vue'
// import Users from '../components/users/Users.vue'
// import Roles from '../components/prower/Roles.vue'
// import Rights from '../components/prower/Rights.vue'
// import Cate from '../components/goods/Cate.vue'
// import Params from '../components/goods/Params.vue'
// import GoodsList from '../components/goods/List.vue'
// import Add from '../components/goods/Add.vue'
// import Order from '../components/orders/Order.vue'
// import Report from '../components/report/Report.vue'

const Login = ()=> import(/* webpackChunkName:"login_home_welcome" */ '../views/Login.vue');
const Home = ()=> import(/* webpackChunkName:"login_home_welcome" */ '../views/Home.vue');
const Welcome = ()=> import(/* webpackChunkName:"login_home_welcome" */ '../components/Welcome.vue');
const Users = ()=> import(/* webpackChunkName:"users" */ '../components/users/Users.vue');
const Roles = ()=> import(/* webpackChunkName:"prower" */ '../components/prower/Roles.vue');
const Rights = ()=> import(/* webpackChunkName:"prower" */ '../components/prower/Rights.vue');
const Cate = ()=> import(/* webpackChunkName:"goods" */ '../components/goods/Cate.vue');
const Params = ()=> import(/* webpackChunkName:"goods" */ '../components/goods/Params.vue');
const GoodsList = ()=> import(/* webpackChunkName:"goods" */ '../components/goods/List.vue');
const Add = ()=> import(/* webpackChunkName:"goods" */ '../components/goods/Add.vue');
const Order = ()=> import(/* webpackChunkName:"orders" */ '../components/orders/Order.vue');
const Report = ()=> import(/* webpackChunkName:"report" */ '../components/report/Report.vue');


Vue.use(VueRouter);

const routes = [
    {path: '*', redirect: '/login'},   //问题:  / 和 * 区别(应用场景有何不同)?
    {path: '/login', component: Login},
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
            {
                path: '/welcome',
                component: Welcome,
            },
            {
                path: '/users',
                component: Users,
            },
            {
                path: '/rights',
                component: Rights,
            },
            {
                path: '/roles',
                component: Roles,
            },
            {
                path: '/categories',
                component: Cate,
            },
            {
                path: '/params',
                component: Params,
            },
            {
                path: '/goods',
                component: GoodsList,
            },
            {
                path: '/goods/add',
                component: Add,
            },
            {
                path: '/orders',
                component: Order,
            },
            {
                path: '/reports',
                component: Report,
            }
        ]
    }
];

const router = new VueRouter({
    routes
});

//路由守卫 登录鉴权
//to,去哪 from,从来来 next是否进入
router.beforeEach((to, from, next) => {
    console.log(to);
    let token = window.sessionStorage.getItem("token");
    if (to.path === "/login") return token ? next("/home") : next();
    if (!token) return next("/login");
    next();
});

export default router
